<?php

class Banners extends Controller
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::Controller();
			
		// Check if the logged user is an administrator
		$this->access_library->check_access();

		// Load needed models, libraries, helpers and language files 
		$this->load->module_model('admin', 'links_model', 'banners');
		
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'links');
	}

	// Public methods
	
	public function exchange()
	{
		$id = $this->input->post('id');
		if($id !=null)
		{
			$banner = $this->banners->get_link($id);
			$status = 0;
			if($banner['status']=='0')
			{
				$status = 1;
			}			
			$this->banners->banner_exchange($id, $status);	
			$row['result'] = 1;
			$row['status'] = $status;
			echo json_encode($row);		
		}
		else
		{
			$row['result'] = 0;
			echo json_encode($row);
		}
	}
	
	public function index()
	{
		$lang_working = load_language_working();
		$data['banners'] = $this->banners->get_banners($lang_working['id']);

		$this->_template['page']	= 'banners/list';

		$this->system_library->load($this->_template['page'], $data, TRUE);
		//$this->load->library('image_lib');
	}

	public function create_link()
	{
		$this->form_validation->set_rules('name', 'lang:form_name', 'required|max_length[50]|xss_clean');
		$this->form_validation->set_rules('url', 'lang:form_url', 'required|xss_clean');
		$this->form_validation->set_rules('target', 'lang:form_target', 'required|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'required|max_length[100]|xss_clean');
		$this->form_validation->set_rules('visible', 'lang:form_visible', 'required|xss_clean');
		if ($this->form_validation->run() == TRUE)
		{
			$this->links->create_link();
			
			$this->session->set_flashdata('message', lang('successfully_created'));

			redirect('admin/links', 'refresh');
		}
		$this->_template['page']	= 'links/create_link';		
		$this->system_library->load($this->_template['page'], null, TRUE);
	}
	public function create()
	{
		$lang_working = load_language_working();
		$this->form_validation->set_rules('name', 'lang:form_name', 'required|max_length[50]|xss_clean');
		$this->form_validation->set_rules('url', 'lang:form_url', 'required|xss_clean');
		$this->form_validation->set_rules('target', 'lang:form_target', 'required|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'required|max_length[100]|xss_clean');
		$this->form_validation->set_rules('visible', 'lang:form_visible', 'required|xss_clean');

		$this->form_validation->set_error_delimiters('', '<br />');
		$config['upload_path'] = './uploads/banners';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '100';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		
		$this->load->library('upload', $config);
		
		// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
		$this->upload->initialize($config);
		if ($this->form_validation->run() == TRUE)
		{
			if ( ! $this->upload->do_upload())
			{
				
				$this->session->set_flashdata('message', $this->upload->display_errors());
				$this->_template['page']	= 'banners/create';
			
				$this->system_library->load($this->_template['page'], null, TRUE);
			}	
			else
			{
				$data =  $this->upload->data();	
				$data =  $this->upload->data();		
					$config = array(
							'source_image'=>'./uploads/banners',
						'create_thumb'=>'./uploads/banners/thumb',
						'maintain_ratio'=>true,
						'width'=>'160',
						'height'=>'79'
					);	
				$this->banners->create_banners($lang_working['id'],$data);
				
				$this->session->set_flashdata('message', lang('successfully_created'));
	
				redirect('admin/banners', 'refresh');
			}
		}	
		else
		{
			$this->_template['page']	= 'banners/create';
				
			$this->system_library->load($this->_template['page'], null, TRUE);
		}
	}

	public function edit($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->form_validation->set_rules('name', 'lang:form_name', 'required|max_length[50]|xss_clean');
		$this->form_validation->set_rules('url', 'lang:form_url', 'required|xss_clean');
		$this->form_validation->set_rules('target', 'lang:form_target', 'required|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'required|max_length[100]|xss_clean');
		$this->form_validation->set_rules('visible', 'lang:form_visible', 'required|xss_clean');

		$this->form_validation->set_error_delimiters('', '<br />');
			
		$data['banner'] = $this->banners->get_link($id);
		//$photo_path = $this->input->post('userfile');		
		$config['upload_path'] = './uploads/banners';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '100';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$photo_path =  $_FILES['userfile']['name'];
		
		$this->load->library('upload', $config);	
		if ($this->form_validation->run() == TRUE)
		{//echo $photo_path;die();
			if($photo_path != '')
			{
				if ( !$this->upload->do_upload())
				{
					echo $this->upload->display_errors();
					$this->session->set_flashdata('message', $this->upload->display_errors());
					$this->_template['page']	= 'banners/edit';
			
					$this->system_library->load($this->_template['page'], null, TRUE);
				}
				else
				{
					$data =  $this->upload->data();		
					$config = array(
						'gd',
						'source_image'=>'./uploads/banners',
						'new_image'=>'./uploads/banners/thumb',
						'maintain_ratio'=>true,
						//'create_thumb'=>true,
						'width'=>160,
						'height'=>79
					);	
					//$this->load->library('image_lib', $config);
					//$this->image_lib->resize();	
					//echo $this->image_lib->display_errors();		
					$this->banners->edit_banner($id,$data);
					$this->session->set_flashdata('message', lang('successfully_edited'));
	
					redirect('admin/banners', 'refresh');
				}
			}
			else
			{
				
				$this->banners->edit_link($id);
				$this->session->set_flashdata('message', lang('successfully_edited'));

				redirect('admin/banners', 'refresh');
			}
		}
		
		$this->_template['page']	= 'banners/edit';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	public function edit_link($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->form_validation->set_rules('name', 'lang:form_name', 'required|max_length[50]|xss_clean');
		$this->form_validation->set_rules('url', 'lang:form_url', 'required|xss_clean');
		$this->form_validation->set_rules('target', 'lang:form_target', 'required|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'required|max_length[100]|xss_clean');
		$this->form_validation->set_rules('visible', 'lang:form_visible', 'required|xss_clean');

		$this->form_validation->set_error_delimiters('', '<br />');
			
		$data['link'] = $this->links->get_link($id);
			
		if ($this->form_validation->run() == TRUE)
		{
			$this->links->edit_link($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));

			redirect('admin/links', 'refresh');
		}
		
		$this->_template['page']	= 'links/edit_link';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	public function delete($id)
	{
		$this->links->delete_link($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/links', 'refresh');
	}
}

/* End of file links.php */
/* Location: ./application/modules/admin/controllers/links.php */